package edu.hdu.android.sudoku;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

public class KeyDialog extends Dialog {

    private static final String TAG = "KeyDialog";

    private final View[] keys = new View[9];
    private final View view;
    private final int[] used;

    KeyDialog(@NonNull Context context, View view, int[] used) {
        super(context);
        this.used = used;
        this.view = view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keypad);
        findViews();
        for (int value : used) {
            if (value != 0) {
                keys[value - 1].setVisibility(View.INVISIBLE);
            }
        }
        setListeners();
    }

    private void refresh(int num) {
        ((DrawView) view).setSelectedNum(num);
        dismiss();
    }

    private void setListeners() {
        for (int i=0; i<keys.length; i++) {
            final int t = i + 1;
            keys[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    refresh(t);
                }
            });
        }
    }

    private void findViews() {
        keys[0] = findViewById(R.id.keypad_1);
        keys[1] = findViewById(R.id.keypad_2);
        keys[2] = findViewById(R.id.keypad_3);
        keys[3] = findViewById(R.id.keypad_4);
        keys[4] = findViewById(R.id.keypad_5);
        keys[5] = findViewById(R.id.keypad_6);
        keys[6] = findViewById(R.id.keypad_7);
        keys[7] = findViewById(R.id.keypad_8);
        keys[8] = findViewById(R.id.keypad_9);
    }
}
